package com.newsoft.frame.codegen.parameter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cw.jeeyt.common.enums.EnumGenerateCodeType;
import com.newsoft.frame.codegen.Configuration;

/**
 * The abstract parameter context factory implementation.
 *
 * @author chenw
 */
public abstract class AbstractParamContextFactoryImpl implements ParamContextFactory {

    protected Logger logger = LoggerFactory.getLogger(AbstractParamContextFactoryImpl.class);

    @Override
    public ParamContext create(String generateType) throws Exception {

        Configuration configuration = new Configuration();
        if (EnumGenerateCodeType.PAGE.getCode()
            .equals(generateType)) {
            configuration = Configuration.getPageConfig();
        } else if (EnumGenerateCodeType.CLASS.getCode()
            .equals(generateType)) {
            configuration = Configuration.getClassConfig();
        } else if (EnumGenerateCodeType.JDBC.getCode()
            .equals(generateType)) {
            configuration = Configuration.getJdbcConfig();
        } else {
            configuration = Configuration.getDefault();
        }
        if (configuration == null) {
            logger.error("Can't get default configuration!");
            return null;
        }

        ParamContext context = new ParamContext();
        context.setJavaOutputPath(configuration.getJavaOutputPath());
        context.setJavaTemplatePath(configuration.getJavaTemplatePath());
        context.setPageOutputPath(configuration.getPageOutputPath());
        context.setPageTemplatePath(configuration.getPageTemplatePath());
        context.setXmlTemplatePath(configuration.getXmlTemplatePath());
        context.setXmlOutputPath(configuration.getXmlOutputPath());

        fulfillContextOnDemand(context, configuration);

        if (logger.isInfoEnabled()) {
            logger.info("Succeed to create param context: " + context);
        }

        return context;
    }

    protected abstract void fulfillContextOnDemand(ParamContext context, Configuration configuration) throws Exception;

}
